﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;

namespace DotNETX.Utility.Resources
{
    public static class FileLoader
    {
        public static bool UserCache = true;
        static Dictionary<string, Bitmap> _bitmapCache = new Dictionary<string, Bitmap>();
        public static Bitmap GetBitmap(string file)
        {
            if (UserCache && _bitmapCache.ContainsKey(file)) return _bitmapCache[file];
            Bitmap bmp = null;

            using (Stream s = GetStream(file))
            {
                Bitmap t = new Bitmap(s);
                bmp = new Bitmap(t);
                _bitmapCache[file] = bmp;
                s.Close();
            }
            return bmp;
        }

        public static Bitmap GetEmbededBitmap(string file)
        {
            return GetEmbededBitmap(file, Assembly.GetCallingAssembly());
        }

        public static Bitmap GetEmbededBitmap(string file, Assembly assembly)
        {
            if (UserCache && _bitmapCache.ContainsKey(file)) return _bitmapCache[file];
            Bitmap bmp = null;
            using (Stream s = GetEmbededStream(file, assembly))
            {
                Bitmap t = new Bitmap(s);
                bmp = new Bitmap(t);
                _bitmapCache[file] = bmp;
                s.Close();
            }
            return bmp;
        }

        public static Bitmap GetEmbededBitmap(string file, Type type)
        {
            if (UserCache && _bitmapCache.ContainsKey(file)) return _bitmapCache[file];
            Bitmap bmp = null;
            using (Stream s = GetEmbededStream(file, type))
            {
                Bitmap t = new Bitmap(s);
                bmp = new Bitmap(t);
                _bitmapCache[file] = bmp;
                s.Close();
            }
            return bmp;
        }

        public static Stream GetStream(string file)
        {
            if (!Path.IsPathRooted(file) && File.Exists(Path.Combine(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), file)))
            {
                return new FileStream(Path.Combine(Path.GetDirectoryName(Assembly.GetCallingAssembly().Location), file), FileMode.Open);
            }
            else if (File.Exists(file))
            {
                return new FileStream(file, FileMode.Open);
            }
            else
            {
                file = file.Replace('\\', '.');
                if (!file.StartsWith("."))
                {
                    file = "." + file;
                }
                return GetEmbededStream(string.Format("{0}{1}", Assembly.GetCallingAssembly().FullName.Split(',')[0], file), Assembly.GetCallingAssembly());
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="file">文件名</param>
        /// <param name="type">类型名</param>
        /// <returns></returns>
        public static Stream GetEmbededStream(string file, Type type)
        {
            Assembly assembly = null;
            if (type == null) assembly = Assembly.GetCallingAssembly();
            else assembly = type.Assembly;
            List<string> resourceNames = new List<string>(assembly.GetManifestResourceNames());
            string fullpath = string.Format("{0}.{1}", type.Namespace, file);
            if (!resourceNames.Contains(fullpath))
            {
                throw new Exception(string.Format("不存在嵌入的资源文件：{0}", fullpath));
            }
            return assembly.GetManifestResourceStream(fullpath);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="file">默认命名空间 + 路径</param>
        /// <param name="assembly"></param>
        /// <returns></returns>
        public static Stream GetEmbededStream(string file, Assembly assembly)
        {
            if (assembly == null) assembly = Assembly.GetCallingAssembly();
            List<string> resourceNames = new List<string>(assembly.GetManifestResourceNames());
            if (!resourceNames.Contains(file))
            {
                throw new Exception(string.Format("不存在嵌入的资源文件：{0}", file));
            }
            return assembly.GetManifestResourceStream(file);
        }
        public static Stream GetEmbededStream(string file)
        {
            return GetEmbededStream(file, Assembly.GetCallingAssembly());
        }
    }
}
